package com.rhb.ca.uzer.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import java.text.*;
import org.apache.struts.action.*;

import com.rhb.ca.role.bean.Role;
import com.rhb.ca.role.bean.RoleQuery;
import com.rhb.ca.uzer.bean.*;
import com.rhb.ca.uzer.business.*;
import com.rhb.ca.uzer.web.struts.form.UzerForm;
import com.rhb.ca.web.struts.action.BaseDispatchAction;


public class UzerControlAction extends BaseDispatchAction {
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		return list(mapping,form,request,response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		UzerForm eForm = (UzerForm)form;

		UzerBusiness ub = this.getUzerBusiness();

		int allcount = this.getUzerBusiness().getAllCount_findByQuery(eForm.getUzerQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getUzerBusiness().findByQuery(eForm.getUzerQuery(),eForm.getStart(),eForm.getCount());
		}else{
			list = (List)new ArrayList();
		}

		eForm.setAllCount(allcount);
		eForm.setList(list);
		return mapping.findForward("list");
	}

	public ActionForward json(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		UzerForm oform = (UzerForm)form;
		String no = request.getParameter("no");
		try{
			if(no==null||"".equals(no)) no = request.getParameter("loginName");
			no = new String(no.getBytes("ISO8859_1"));
		}catch(Exception e){}
		if(no!=null&&!"".equals(no)) oform.getUzer().setLoginName(no);

		oform.setUzer(this.getUzerBusiness().findByLoginName(oform.getUzer().getLoginName()));
		return mapping.findForward("json");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		UzerForm oform = (UzerForm)form;
		oform.setUzer(new Uzer());
		oform.setRolePksSelected(null);
		oform.setRolesNotSelected(this.getRoleBusiness().findByQuery(new RoleQuery(), 0, 100));
		return mapping.findForward("add");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		UzerForm oform = (UzerForm)form;
		oform.setRolePksSelected(null);
		oform.setUzer(this.getUzerBusiness().findByPk(oform.getUzer().getUzerPk()));
		oform.setRolesNotSelected(this.getRoleBusiness().findByQuery(new RoleQuery(), 0, 100));

		return mapping.findForward("edit");
	}
	
	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		UzerForm oform = (UzerForm)form;
		oform.setUzer(this.getUzerBusiness().findByPk(oform.getUzer().getUzerPk()));
		return mapping.findForward("view");
	}
	
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		return mapping.findForward("del");
	}
}
